package com.htg.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


public class HtgOneButtonDialog {

    private Context cxt;

    private AlertDialog alertDialog = null;

    private int widthResId;
    private String title;
    private String content;
    private String btText;
    private int btTextColor;
    private View.OnClickListener onClickListener;

    public HtgOneButtonDialog() {
        widthResId = R.dimen.dp_280;
    }

    public HtgOneButtonDialog(int widthResId) {
        this.widthResId = widthResId;
    }

    public HtgOneButtonDialog title(String title, String content, String btText, int btTextColor, View.OnClickListener onClickListener){
        this.widthResId = widthResId;
        this.title = title;
        this.content = content;
        this.btText = btText;
        this.btTextColor = btTextColor;
        this.onClickListener = onClickListener;
        return this;
    }

    public void show(Context context){
        if (alertDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(cxt);
            alertDialog = builder.create();
        } else {
            if (alertDialog.isShowing()) {
                return;
            }
        }
        alertDialog.show();
        View view = LayoutInflater.from(cxt).inflate(R.layout.dialog_standard_onebutton, null);
        Window window = alertDialog.getWindow();
        window.setBackgroundDrawableResource(R.drawable.shape_white_rounded_12dp);
        WindowManager.LayoutParams params = window.getAttributes();
        int width = cxt.getResources().getDimensionPixelSize(widthResId);

        params.width = width;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        //window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
        window.setContentView(view);

        TextView tvTitle = view.findViewById(R.id.tv_dialog_standard_onebutton_title);
        TextView tvContent = view.findViewById(R.id.tv_dialog_standard_onebutton_content);
        TextView tvButton = view.findViewById(R.id.tv_dialog_standard_onebutton_button);


        tvTitle.setText(title);
        tvContent.setText(content);
        tvButton.setText(btText);
        tvButton.setTextColor(btTextColor);

        tvButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                if (onClickListener != null) {
                    onClickListener.onClick(v);
                }
            }
        });
    }

}
